<?php


/**
 * 公共管理
 * Enter description here ...
 * @author Admin
 *
 */
class PublicAction extends CommonAction {
	
	//框架上
	function top(){
		$admin = $_SESSION['admin'];   //登录超管用户
		
		$this->assign('admin',$admin);
		$this->display();
	}

	//框架左
	function left(){
		$this->display();
	}
	
	//框架右
	function right(){
		$this->display();
	}

	
	/**
	 * 欢迎页面
	 * Enter description here ...
	 */
	function welcome(){
	  $admin = $_SESSION['admin'];  //登录超管用户
	  $info = array(
            '操作系统'=>PHP_OS,
            '运行环境'=>$_SERVER["SERVER_SOFTWARE"],
	  		'当前登录管理员'=>"用户名:<font color='red'>{$admin['name']}</font> &nbsp;&nbsp;最后登录时间:<font color='red'>".date("Y年n月j日 H:i:s",$admin['lastlogin'])."</font>&nbsp;&nbsp;登录IP:<font color='red'>{$admin['lastlogin_ip']}</font>&nbsp;&nbsp;登录次数:<font color='red'>{$admin['logincount']}</font> ",
            //'上传附件限制'=>ini_get('upload_max_filesize'),
            //'执行时间限制'=>ini_get('max_execution_time').'秒',
            '服务器时间'=>date("Y年n月j日 H:i:s"),
           // '北京时间'=>gmdate("Y年n月j日 H:i:s",time()+8*3600),
           // '服务器域名/IP'=>$_SERVER['SERVER_NAME'].' [ '.gethostbyname($_SERVER['SERVER_NAME']).' ]',
            '剩余空间'=>round((@disk_free_space(".")/(1024*1024)),2).'M',
            );
            
        $this->assign('info',$info);
        
        $this->display();
	}

	
	
	
	/**
	 * 登陆状态认证
	 * Enter description here ...
	 */
	function login(){
		if(!isset($_SESSION[C('USER_AUTH_KEY')])) { //没有当前用户则默认未登陆
			$this->display(); 
		}else{  //已登陆跳转管理首页
			$this->redirect('Index/index');
		}
	}
	
	
	
	/**
	 * 注销系统
	 * Enter description here ...
	 */
	function logout(){
		if(isset($_SESSION[C('USER_AUTH_KEY')])) {
			unset($_SESSION[C('USER_AUTH_KEY')]);
			unset($_SESSION[C('ADMIN_AUTH_KEY')]);
			unset($_SESSION);
			
			session_destroy();  //销毁session
			
            $this->assign("jumpUrl",'__URL__/login');
            
            $this->success('登出成功！');
        }else {
            $this->error('已经登出！');
        }
	}
	
	
	
	/**
	 * 登陆验证码
	 * Enter description here ...
	 */
	function verify(){
		import("@.ORG.Image");

		Image::buildImageVerify(4,1,"png");
	}


	
	/**
	 * 后台登陆认证
	 * Enter description here ...
	 */
	function checkLogin(){
		//获取提交过来数据
		$data = Init_GP(array("username","password","verifycode"));
		
		$this->assign("jumpUrl",__APP__);
		
		if (empty($data['username'])) {
			$this->error('账号不能为空');
		}elseif(empty($data['password'])){
			$this->error('密码不能为空');
		}elseif(empty($data['verifycode'])){
			$this->error('验证码不能为空');
		}elseif(md5($data['verifycode']) != $_SESSION['verify']){
			$this->error('验证码不正确');
		}
		
		
		//引入权限RBAC
		import ( '@.ORG.RBAC' );
		$where = array(
			'name'=>$data['username']
		);
		
		
		$admin = D("Admin");
		$adminInfo = RBAC::authenticate($where);

		if ($adminInfo === false) {
			$this->error('用户名不存在或已禁用！');
		}else {
			if ($adminInfo['state'] != 1)
				$this->error('此用户已禁用');
			if ($adminInfo['name'] != $data['username'])
				$this->error('账号或密码错误');
			if ($adminInfo['password'] != md5($data['password']))
				$this->error('账号或密码错误');
			
			$_SESSION[C("USER_AUTH_KEY")] = $adminInfo['aid'];   //管理员主键
				
			if ($adminInfo['name'] == "admin") {
				$_SESSION[C("ADMIN_AUTH_KEY")] = true;
			}
			
			$adminInfo['lastlogin'] = time();  //最后登录时间
			$adminInfo['logincount'] = $adminInfo['logincount'] + 1;  //登录次数
			$adminInfo['lastlogin_ip'] = get_client_ip();  //登录IP
			$admin->save($adminInfo);   //保存管理员登陆信息
			
			$_SESSION['admin'] = $adminInfo;    //设置登录session信息
			
			RBAC::saveAccessList ();
			
			$this->success('登录成功！');
		}
		
	}

	
	
	
	
	
	
	/**
	 * 清空缓存
	 * Enter description here ...
	 */
	function clearCache(){
		$dir1 = RUNTIME_PATH;
		$dir2 = ROOT_PATH."/app/Runtime/";
		$info = $this->_clearCacheOperation($dir1);
		$info = $this->_clearCacheOperation($dir2);
		$this->assign("jumpUrl","__URL__/welcome");
		$v1 = @rmdir($dir1);
		$v2 = @rmdir($dir2);
		$cache = $this->_newCache();
		$cache->clear();
		
		if($v1 || $v2) {
			$this->success('清空缓存成功!');
		} else {
			$this->error('清空缓存失败!');
		}
	}
	
	
	/**
	 * 
	 * Enter description here ...
	 * @param unknown_type $dir
	 */
	function _clearCacheOperation($dir){
		$dh=opendir($dir);
		while ($file=readdir($dh)) {
			if($file!="." && $file!="..") {
				$fullpath=$dir."/".$file;
				if(!is_dir($fullpath)) {
					@unlink($fullpath);
				} else {
					$this->_clearCacheOperation($fullpath);
					@rmdir($fullpath);
				}
			}
		}
		closedir($dh);
	}
	
	
	
	/**
	 * 创建缓存
	 * Enter description here ...
	 */	
	public function _newCache(){
    	import("@.ORG.Cache");
    	$cache = new Cache();
    	$config = C('SYSCONFIG');
    	$cache_config = array(
    		'prefix'=>$config['memcache_prefix'],
    		'memcache'=>array(
    						'server'=>$config['memcache_server'],
    						'pconnect'=>$config['memcache_pconnect'],
    						'port'=>$config['memcache_port']
    					),
    		'eaccelerator'=>$config['eaccelerator'],
    		'xcache'=>$config['xcache']
    	);
    	
		$cache->init($cache_config);

		return $cache;
    }


}
?>